package com.app.micaihu.view.main.msgcenter.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.AppBaseAdapter;
import com.app.micaihu.bean.message.MsgList;
import com.app.micaihu.bean.message.PlaceListBean;
import com.app.micaihu.bean.news.NewsEntity;
import com.app.micaihu.configure.Config;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.view.main.msgcenter.MsgNoticeListActivity;
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;
import com.app.micaihu.view.user.userinfo.activity.WebActivity;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.view.expression.listener.Clickable;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Guo on 2016/12/1.
 * <p>
 * <p>
 * 系统消息的适配器
 */

public class MsgNoticeAdapter extends AppBaseAdapter<MsgList> implements View.OnClickListener, Clickable.OneObjectClickListener {

    private int color = AppApplication.getInstance().getResources().getColor(R.color.common_font_color_8);
    private int colorPoint = AppApplication.getInstance().getResources().getColor(R.color.common_font_color_9);
    private NewsEntity newsEntity;

    public MsgNoticeAdapter(List<MsgList> list, Context context) {
        super(list, context);
    }


    @Override
    public View creatView(int position, View convertView, ViewGroup parent) {
        ViewH viewH = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_msg_notice, null);
            viewH = new ViewH(convertView);
            convertView.setTag(viewH);
        } else {
            viewH = (ViewH) convertView.getTag();
        }
        MsgList msgList = list.get(position);
        if (msgList != null) {
            viewH.tvTime.setText(msgList.getPublishTime());
            LoadImageUtils.getInstance().loadCircleImage(viewH.civHeader, msgList.getHeadPic());
            if (TextUtils.isEmpty(msgList.getBanner())) {//没有广告图
                viewH.civThumb.setVisibility(View.GONE);
            } else {
                viewH.civThumb.setVisibility(View.VISIBLE);
                LoadImageUtils.getInstance().loadImage(viewH.civThumb, msgList.getBanner());
            }
            if (TextUtils.isEmpty(msgList.getTitle())) {//如果标题是空的,正文为黑色,否则,正文为灰色
                viewH.tvTitle.setVisibility(View.GONE);
                viewH.tvDesc.setTextColor(parent.getContext().getResources().getColor(R.color.common_font_color_1));
            } else {
                viewH.tvTitle.setVisibility(View.VISIBLE);
                viewH.tvTitle.setText(msgList.getTitle());
                viewH.tvDesc.setTextColor(parent.getContext().getResources().getColor(R.color.common_font_color_3));
            }

            if (TextUtils.isEmpty(msgList.getComment())) {
                viewH.tvDesc.setVisibility(View.GONE);
            } else {
                viewH.tvDesc.setVisibility(View.VISIBLE);


                if (msgList.getPlaceList() != null && !msgList.getPlaceList().isEmpty() && !TextUtils.isEmpty(msgList.getPlaceHolder())) {
                    setColorText(viewH, msgList);
                } else {
                    viewH.tvDesc.setText(msgList.getComment());
                }


            }


            if (TextUtils.isEmpty(msgList.getJumpWords())) {
                viewH.tvGoTo.setVisibility(View.GONE);

            } else {
                viewH.tvGoTo.setVisibility(View.VISIBLE);
                viewH.tvGoTo.setText(msgList.getJumpWords());
            }

            if (viewH.civThumb.getVisibility() == View.VISIBLE) {
                viewH.llContainer.setTag(msgList);
            }

            if (viewH.tvGoTo.getVisibility() == View.VISIBLE) {
                viewH.tvGoTo.setTag(msgList);

            }


        }


        return convertView;
    }


    /**
     * 设置变色字体
     */
    private void setColorText(ViewH viewH, MsgList msgList) {
        String comment = msgList.getComment();
        String placeHolder = msgList.getPlaceHolder();
        List<PlaceListBean> placeList = msgList.getPlaceList();
        ArrayList<Integer> indexList = new ArrayList<>();
        //添加 索引集合 以及替换关键字
        for (int i = 0; i < msgList.getPlaceList().size(); i++) {
            if (placeList.get(i) != null
                    && comment.indexOf(placeHolder) != -1) {
                indexList.add(comment.indexOf(placeHolder));

                comment = comment.replaceFirst(placeHolder, " " + placeList.get(i).getText() + " ");
            }
        }

        SpannableString spannableString = new SpannableString(comment);
        for (int i = 0; i < indexList.size(); i++) {
            if (msgList.getPlaceList().get(i) != null) {
                if (!TextUtils.equals(msgList.getPlaceList().get(i).getType(), TypeConstant.MSG_NOTICE_REASON)) {
                    spannableString.setSpan(new Clickable(this, msgList.getPlaceList().get(i)), indexList.get(i), indexList.get(i) + placeList.get(i).getText().length() + 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    spannableString.setSpan(new ForegroundColorSpan(color), indexList.get(i), indexList.get(i) + placeList.get(i).getText().length() + 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                } else {
                    spannableString.setSpan(new ForegroundColorSpan(colorPoint), indexList.get(i), indexList.get(i) + placeList.get(i).getText().length() + 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                }

            }
        }
        viewH.tvDesc.setHighlightColor(Color.TRANSPARENT);
        viewH.tvDesc.setText(spannableString);
        viewH.tvDesc.setMovementMethod(LinkMovementMethod.getInstance());


    }

    @Override
    public void click(Object parms) {

        if (parms == null) return;
        PlaceListBean placeListBean = (PlaceListBean) parms;
        if (TextUtils.isEmpty(placeListBean.getType()) || context == null) return;
        switch (placeListBean.getType()) {
            case TypeConstant.MSG_NOTICE_USER:
                if (!TextUtils.isEmpty(placeListBean.getId())) {
                    //点击评论用户昵称
                    Intent intent = new Intent(context, MyHomepageDynamicActivity.class);
                    intent.putExtra(Config.Extra.PARAMETER1, placeListBean.getId());
                    context.startActivity(intent);
                }

                break;
            case TypeConstant.MSG_NOTICE_NEWS:
                if (newsEntity == null) {
                    newsEntity = new NewsEntity();
                }
                newsEntity.setArticleType(placeListBean.getArticleType());
                newsEntity.setJumpUrl(placeListBean.getJumpUrl());
                newsEntity.setArticleId(placeListBean.getId());
                TypeConstant.openTypeDetail(newsEntity, (Activity) context, TypeConstant.SOURCETYPE_SYSMSG);
                break;
            default:
                return;
        }

    }

    @Override
    public void onClick(View view) {
        if (view == null) return;
        switch (view.getId()) {
            case R.id.ll_container://整体跳转
            case R.id.tv_goto://点击查看跳转
                goTo(view);
                break;
        }
    }


    /**
     * 跳转
     *
     * @param view
     */
    private void goTo(View view) {
        Object tag = view.getTag();
        if (tag != null && tag instanceof MsgList) {
            MsgList msgList = (MsgList) tag;

            String jumpType = msgList.getJumpType();
            if (TextUtils.isEmpty(jumpType)) return;
            if (context instanceof MsgNoticeListActivity) {
                if (TextUtils.equals(jumpType, "3")) {//首页资讯
                    ((MsgNoticeListActivity) context).setResult(Activity.RESULT_OK);
                    ((MsgNoticeListActivity) context).onBackPressed();
                } else if (TextUtils.equals(jumpType, "4")) {//H5
                    Intent intent = new Intent(context, WebActivity.class);
                    intent.putExtra("title", "");
                    intent.putExtra("url", msgList.getJumpUrl());
                    context.startActivity(intent);

                } else if (TextUtils.equals(jumpType, "5")) {//资讯内
                    NewsEntity newsEntity = new NewsEntity();
                    newsEntity.setArticleType(msgList.getArticleType());
                    newsEntity.setArticleId(msgList.getArticleId());
                    TypeConstant.openTypeDetail(newsEntity, (Activity) context, TypeConstant.SOURCETYPE_SYSMSG);

//                    Intent intent = new Intent(context, NewsDetailActivity.class);
//                    intent.putExtra(BaseActivity.PARAMETER1, msgList.getArticleId());
//                    intent.putExtra(BaseActivity.PARAMETER2, TypeConstant.SOURCETYPE_SYSMSG);
//                    context.startActivity(intent);
                }
            }


        }


    }


    class ViewH {
        TextView tvTitle;
        TextView tvDesc;
        TextView tvGoTo;
        TextView tvTime;
        CustomImageView civHeader;
        CustomImageView civThumb;

        LinearLayout llContainer;


        public ViewH(View convertView) {
            tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
            tvDesc = (TextView) convertView.findViewById(R.id.tv_desc);
            tvGoTo = (TextView) convertView.findViewById(R.id.tv_goto);
            tvTime = (TextView) convertView.findViewById(R.id.tv_time);

            civHeader = (CustomImageView) convertView.findViewById(R.id.civ_head);
            civThumb = (CustomImageView) convertView.findViewById(R.id.civ_thumb);

            llContainer = (LinearLayout) convertView.findViewById(R.id.ll_container);

            tvGoTo.setOnClickListener(MsgNoticeAdapter.this);
            llContainer.setOnClickListener(MsgNoticeAdapter.this);
        }
    }
}
